package design.pattern.Lu18MementoPattern;

import cn.hutool.core.clone.CloneSupport;
import cn.hutool.core.util.ObjectUtil;

import java.io.Serializable;

/**
 * @program JavaBase
 * @description: 备忘录模式 + 原型模式
 *  clone方式的备忘录模式，简单场景好用，复杂场景下，程序比较混乱
 * @author: zhanglu
 * @create: 2020-04-10 16:04:00
 */
public class MementoPattern03 {

    public static void main(String[] args) {
        Originator originator = new Originator();

        originator.setState("还好");
        originator.createMemento();

        originator.print();
        originator.changeState();
        originator.print();
        originator.restoreMemento();
        originator.print();

    }

}

class Originator extends CloneSupport<Originator> implements Serializable {

    private Originator backup;

    private String state;

    public void changeState(){
        this.state = "不太好";
    }

    public void setState(String state){
        this.state = state;
    }

    public String getState(){
        return this.state;
    }

    public void print(){
        System.out.println(this.state);
    }

    public void createMemento(){
        this.backup = ObjectUtil.clone(this);
    }

    public void restoreMemento(){
        this.setState(this.backup.getState());
    }

}

